package com.e9where.canpoint.customrecycler.recycler.item;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;

/**
 * Created by Administrator on 2017/12/4.
 * 刷新头部
 */

public class CustomHead extends FrameLayout implements PtrUIHandler{
    private TextView textView;

    public CustomHead(@NonNull Context context) {
        super(context);
        init();
    }

    private void init() {
        textView = new TextView(getContext());
        //文字颜色
        textView.setTextColor(Color.parseColor("#666666"));
        //背景颜色
        textView.setBackgroundColor(Color.parseColor("#f1f1f1"));
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
        textView.setText("下拉刷新");
        addView(textView, new LayoutParams(LayoutParams.MATCH_PARENT, dip2px(50)));
    }

    //设置刷新动画的大小
    private  int dip2px(int dipValue) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }


    //初始化状态
    @Override
    public void onUIReset(PtrFrameLayout frame) {

    }

    //开始向下拉的时候调用
    @Override
    public void onUIRefreshPrepare(PtrFrameLayout frame) {

    }

    //刷新过程时调用
    @Override
    public void onUIRefreshBegin(PtrFrameLayout frame) {
        //可以不断的改变动画效果以及切换显示的控件
        //判断是否可以刷新
        if (frame.isPullToRefresh()) {
            textView.setText("释放刷新");
        } else {
            textView.setText("加载中...");
        }
    }


    //刷新完成后调用,向上移动时调用
    @Override
    public void onUIRefreshComplete(PtrFrameLayout frame) {
        textView.setText("刷新完成");
    }

    //重复下拉
    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
        //在同一次下拉中不断向上向下移动,这里可以不断改变显示效果
        //示例代码:  可以当模板使用
        final int mOffsetToRefresh = frame.getOffsetToRefresh();
        final int currentPos = ptrIndicator.getCurrentPosY();  //获取到下拉的高度
        final int lastPos = ptrIndicator.getLastPosY();      //最大下拉的高度
        //根据下拉的位置进行控件的显示
        if (currentPos < mOffsetToRefresh && lastPos >= mOffsetToRefresh) {
            if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
                crossRotateLineFromBottomUnderTouch(frame); //调用方法
            }
        } else if (currentPos > mOffsetToRefresh && lastPos <= mOffsetToRefresh) {
            if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
                crossRotateLineFromTopUnderTouch(frame);  //调用方法
            }
        }
    }

    //下拉到可以刷新时显示
    private void crossRotateLineFromTopUnderTouch(PtrFrameLayout frame) {
        if (!frame.isPullToRefresh()) {
            textView.setText("释放刷新");
        }
    }


    //动态改变文字
    private void crossRotateLineFromBottomUnderTouch(PtrFrameLayout frame) {
        if (frame.isPullToRefresh()) {
            textView.setText("释放刷新");
        } else {
            textView.setText("下拉加载");
        }
    }
}
